/**
 * Created by Administrator on 2017/4/18.
 */
var Countdown = require('../../../../src/libs/countdown');
var UFormatter = require('../../../../src/core/utils/formatter.js');

var InvestAction = React.createClass({
    getDefaultProps:function(){
        return {
            data:{
                minFlag:true,
                //flag:this.props.data
                flag:{
                    login:true,
                    loginUrl:"",
                    confirmUrl:"",
                    ProjectStatus:1,
                    JMBeginTime:"",
                    ProjectBeginTime:"",

                    MinInvestPartsCount:"",
                    MaxInvestPartsCount:"",
                    EachPartAmount:""
                }
            }
        }
    },
    _clickHandle:function(){
        var jmTime = new Date(UFormatter.formatTime(this.props.data.flag.JMBeginTime,"yyyy/mm/dd hh:mm:ss")).getTime();
        if(this.props.data.flag.ProjectStatus == 3 || jmTime < new Date().getTime()){
            return;
        }
        var max = Number(this.props.data.flag.MaxInvestPartsCount),
            min = Number(this.props.data.flag.MinInvestPartsCount),
            each = Number(this.props.data.flag.EachPartAmount),
            surplus = max * each,
            least = min * each;
        if(surplus < least){
            UDialog.alert({
                title:'温馨提示',
                content:'您在此项目的剩余可投金额不足，请选择其他项目'
            });
        }else{
            //console.log(1)
        }

        var confirmUrl = this.props.data.flag.confirmUrl;
        if(typeof confirmUrl == 'function' && confirmUrl.constructor == Function){
            confirmUrl();
        }
    },
    render:function(){
        var _this = this;
        var item = this.props.data.flag;
        if(item.login == undefined){
            item.login = true;
        }
        //按钮文案
        var arrBtn = [
                {
                    text:"投标中",
                    className:"btn-bidnow"
                },
                {
                    text:"预热中",
                    className:"btn-wait"
                },
                {
                    text:"已满标",
                    className:"btn-full"
                },
                {
                    text:"计息中",
                    className:"btn-full"
                },
                {
                    text:"还款中",
                    className:"btn-full"
                },
                {
                    text:"已还款",
                    className:"btn-full"
                }
            ];
        if(!this.props.data.minFlag){
            var jmTime = new Date(UFormatter.formatTime(this.props.data.flag.JMBeginTime,"yyyy/mm/dd hh:mm:ss")).getTime();
            if(jmTime < new Date().getTime()){
                arrBtn[2].className = "btn-disabled";
                arrBtn[0] = arrBtn[2];
            }else{
                arrBtn[0].text = "马上投资";
            }
        }else{
            item.login ? arrBtn[0].text = "马上投资": arrBtn[0].text = "马上登录投资"
        }

        var _url = "";
        if(item.login){
            if(item.confirmUrl && typeof item.confirmUrl == "string"){
                _url = item.confirmUrl;
            }else{
                _url = "javascript:void(0)";
            }
        }else{
            _url = item.loginUrl;
        }

        return(
            <div className={this.props.className}>
                {this.props.data.minFlag ?
                    <a className={"btn "+arrBtn[item.ProjectStatus - 1].className} href={item.confirmUrl}>
                        {arrBtn[item.ProjectStatus - 1].text}
                    </a> :
                    <a className={"btn "+arrBtn[item.ProjectStatus - 1].className}
                       href={_url}
                       onclick={_this._clickHandle.bind(_this)}>
                        {arrBtn[item.ProjectStatus - 1].text}
                    </a>
                }
                {
                    item.ProjectStatus == 1 ?
                        <p>距项目结束还有：<Countdown timeRemaining={item.JMBeginTime} tpl="{d}天{h}小时{m}分{s}秒"/></p> :
                    item.ProjectStatus == 2 ?
                        <p>距项目开始还有：<Countdown timeRemaining={item.ProjectBeginTime} tpl="{d}天{h}小时{m}分{s}秒"/></p> : ""
                }
            </div>
        )
    }
});
module.exports = InvestAction;